﻿Public Class Service

	Shared Function Aylar(bolge As Integer) As Recordset
		Return OpenSQL("select convert(char(6),basvurutar,112),count(*) from istihdam where bolge = @bolge group by convert(char(6),basvurutar,112) order by 1 desc", bolge)
	End Function

	Shared Function Liste(bolge As Integer, view As Integer, month As String) As Recordset
		Q("select rowid,kimlik,adi,soyadi,dogumtar,basvurutar from istihdam where bolge = @bolge")
		Select Case view
			Case 0 : Q("and convert(char(6),basvurutar,112) = '" & Today.ToString("yyyyMM") & "'")
			Case 1 : Q("and convert(char(6),basvurutar,112) = '" & Today.AddMonths(-1).ToString("yyyyMM") & "'")
			Case 3 : Q("and convert(char(6),basvurutar,112) = '" & month.Replace("/", "") & "'")
			Case 4 : Q("and rowid > (select max(rowid) from istihdam) - 50")
		End Select
		Select Case view
			Case 4 : Q("order by rowid desc")
			Case Else : Q("order by adi,soyadi")
		End Select
		Return OpenSQL("*", bolge)
	End Function

	Public Class Combo

		Shared Function Egitim() As Recordset
			Return OpenSQL("select kod,kod+'. '+adi as adi from egitim union all select '','' order by kod")
		End Function

		Shared Function Meslek() As Recordset
			Return OpenSQL("select kod,kod+' '+adi as adi from meslek where len(kod)>1 union all select '','' order by kod")
		End Function

		Shared Function Faaliyet() As Recordset
			Return OpenSQL("select kod,kod+'. '+adi as adi from faaliyet union all select '','' order by kod")
		End Function

	End Class

	Class Meslek

		Shared Function Liste() As Recordset
			Return OpenSQL("select * from meslek where len(kod) > 2 order by kod")
		End Function

		Shared Function Kullanimda(kod As String) As Boolean
			Return Not OpenSQL("select * from istihdam where meslek = @meslek",kod).eof
		End Function

	End Class

End Class

Public Class Field

	Dim Table As Table
	Dim Name As String

	Sub New(Table As Table, Name As String)
		Me.Table = Table
		Me.Name = Name
	End Sub

	Property Value As Object
		Get
			Return Table.Rs(Name)
		End Get
		Set(Value As Object)
			Table.Rs(Name) = Value
		End Set
	End Property

	ReadOnly Property Empty As Boolean
		Get
			If Table.Rs(Name) Is Nothing Then Return True
			If IsDBNull(Table.Rs(Name)) Then Return True
			Return False
		End Get
	End Property

End Class

Public Class Table

	Public Rs As Recordset
	Public rowid As Object
	Public Table As String
	Public Bindings As New SortedList(Of String, Object)

	Dim HasIdentity As Boolean = False

	Sub New(Table As String)
		Me.Table = Table
	End Sub

	Sub Bind(FieldName As String, Control As Control)
		Bindings.Add(FieldName, Control)
	End Sub

	Sub Bind(Control As Control)
		If TypeOf Control Is Panel Then
			For Each SubControl In CType(Control, Panel).Controls
				Bind(SubControl)
			Next
		Else
			Bindings.Add(Control.Name, Control)
		End If
	End Sub

	Sub Bind(Form As Form)
		For Each Control In Form.Controls
			Bind(Control)
		Next
	End Sub

	Sub Bind(ParamArray Controls As Control())
		For Each Control In Controls
			Bindings.Add(Control.Name, Control)
		Next
	End Sub

	Sub Save()
		If Rs Is Nothing Then
			Rs = OpenSQL("select * from " & Table & " where 1 = 0")
			Rs.AddNew()
		End If
		For Each Binding In Bindings
			Dim Value As Object = Nothing
			Dim Found As Boolean = False
			If TypeOf Binding.Value Is TextBox Then
				Value = CType(Binding.Value, TextBox).Text
				Found = True
			ElseIf TypeOf Binding.Value Is DateTimePicker Then
				Value = CType(Binding.Value, DateTimePicker).Value.Date
				Found = True
			ElseIf TypeOf Binding.Value Is ComboBox Then
				Value = CType(Binding.Value, ComboBox).SelectedValue
				Found = True
			End If
			If Found Then
				Dim Args As New UpdatingEventArgs
				Args.Field = Binding.Key
				Args.Value = Value
				RaiseEvent Updating(Me, Args)
				If Args.Cancel Then Return
				Rs(Binding.Key) = Args.Value
			End If
		Next
		Rs.Save()
		If HasIdentity AndAlso rowid = 0 Then rowid = OpenSQL("select @@identity")(0)
	End Sub

	Class UpdatingEventArgs : Inherits EventArgs
		Public Field As String
		Public Value As Object
		Public Cancel As Boolean
	End Class

	Event Updating(sender As Object, e As UpdatingEventArgs)

	Sub Read(rowid As Integer)
		HasIdentity = True
		Read(rowid, "rowid")
	End Sub

	Sub Read(kod As Object, key As String)
		Me.rowid = kod
		Rs = OpenSQL("select * from " & Table & " where " & key & " = @kod", rowid)
		For Each Binding In Bindings
			If TypeOf Binding.Value Is TextBox Then
				CType(Binding.Value, TextBox).Text = Rs(Binding.Key)
			ElseIf TypeOf Binding.Value Is DateTimePicker Then
				CType(Binding.Value, DateTimePicker).Value = Rs(Binding.Key)
			ElseIf TypeOf Binding.Value Is ComboBox Then
				CType(Binding.Value, ComboBox).SelectedValue = Rs(Binding.Key)
			End If
		Next
	End Sub

	Shared Sub Delete(table As String, rowid As Integer)
		Delete(table, rowid, "rowid")
	End Sub

	Shared Sub Delete(table As String, kod As Object, key As String)
		ExecSQL("delete from " & table & " where " & key & " = @kod", kod)
	End Sub

End Class
